package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.service.MarketCategoryService;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.MarketCategoryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("admin/category")
public class AdminCategoryController {
    @Autowired
    MarketCategoryService categoryService;

    @GetMapping("l1")
    public Object l1() {
        Map<String, Object> data = categoryService.primaryCategoryList();
        return ResponseUtil.ok(data);
    }

    @GetMapping("list")
    public Object list() {
        Map<String, Object> data = categoryService.list();
        return ResponseUtil.ok(data);
    }

    @PostMapping("delete")
    public Object delete(@RequestBody MarketCategoryVo marketCategoryVo) {
//        String requestBody = req.getReader().readLine();
//        String idParam = JacksonUtil.parseString(requestBody, "id");
//        Integer id = null;

        // 校验

        // 修改数据库中的数据
        categoryService.delete(marketCategoryVo.getId());
        // 返回响应信息
        return ResponseUtil.ok();
    }

    @PostMapping("update")
    public Object update(@RequestBody Map map) {
        Integer id = (Integer) map.get("id");
        String name = (String) map.get("name");
        String keywords = (String) map.get("keywords");
        String desc = (String) map.get("desc");
        String iconUrl = (String) map.get("iconUrl");
        String picUrl = (String) map.get("picUrl");
        String level = (String) map.get("level");
        Integer pid = (Integer) map.get("pid");

        // 校验
        if ("L2".equals(level) && pid == null) {
            // 修改二级类目时pid不能为空
            return ResponseUtil.badArgument();
        }
        if (StringUtils.isEmpty(name) || name.isEmpty()) {
            return ResponseUtil.fail(404, "类目名称不能为空");
        }
        if ("L2".equals(level) && map.get("children") == null) {
            // 一级目录修改为二级目录时必须保证它没有子目录
            return ResponseUtil.badArgumentValue();
        }

        // 创建用于更新数据库数据的对象
        MarketCategory updateCategory = new MarketCategory();
        updateCategory.setId(id);
        updateCategory.setName(name);
        updateCategory.setKeywords(keywords);
        updateCategory.setDesc(desc);
        updateCategory.setIconUrl(iconUrl);
        updateCategory.setPicUrl(picUrl);
        updateCategory.setLevel(level);
        updateCategory.setPid(pid);

        // 修改数据库中的数据
        categoryService.update(updateCategory);
        // 返回响应信息
        return ResponseUtil.ok();
    }

    @PostMapping("create")
    public Object create(@RequestBody MarketCategory marketCategory) {
        // 获取请求参数

        //校验
        if (StringUtils.isEmpty(marketCategory.getName()) || marketCategory.getName().isEmpty()) {
            return ResponseUtil.fail(404, "类目名称不能为空");
        }

        // 设置对象属性
        marketCategory.setDeleted(false);
        marketCategory.setSortOrder((byte)50);
        marketCategory.setAddTime(LocalDateTime.now());
        marketCategory.setUpdateTime(LocalDateTime.now());
        // 将对象添加到数据库
        categoryService.create(marketCategory);
        // 返回响应
        Map<String, Object> data = new HashMap<>();
        data.put("id", marketCategory.getId());
        data.put("name", marketCategory.getName());
        data.put("keywords", marketCategory.getKeywords());
        data.put("desc", marketCategory.getDesc());
        data.put("pid", marketCategory.getPid());
        data.put("iconUrl", marketCategory.getIconUrl());
        data.put("picUrl", marketCategory.getPicUrl());
        data.put("level", marketCategory.getLevel());
        data.put("addTime", marketCategory.getAddTime());
        data.put("updateTime", marketCategory.getUpdateTime());
        return ResponseUtil.ok(data);
    }
}
